package slave.sspGUI;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.bluetooth.BluetoothStateException;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import com.intel.bluetooth.BlueCoveImpl;
import slave.BluetoothStack;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class BootStrapDevice extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JPanel jPanel1;
	private JLabel jLabel3;
	private JLabel jLabel5;
	private JButton jButton1;
	private JComboBox helperDeviceCombo;
	private JComboBox mainDeviceCombo;
	private JLabel jLabel2;
	private JLabel jLabel1;
	java.awt.Toolkit toolkit;
	private StreamProviderMonitor spm = null;


	public BootStrapDevice(StreamProviderMonitor streamProviderMonitor) {
		super();
	    this.spm=streamProviderMonitor;
		initGUI();
		getDevice();
	}
	
	private void initGUI() {
		try {
			ImageIcon image = new ImageIcon("./media/device.png");

			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			setTitle("Slave Stream Provider Bootstrap");
            setSize(330,150);
			setResizable(false);
			toolkit = Toolkit.getDefaultToolkit();
			setLocation((int)(toolkit.getScreenSize().getWidth()/2-this.getWidth()/2),(int)(toolkit.getScreenSize().getHeight()/2-this.getHeight()/2));
			getContentPane().setLayout(null);
			setVisible(true);
			{
				jPanel1 = new JPanel();

				getContentPane().add(jPanel1, "Center");
				jPanel1.setLayout(null);
				jPanel1.setBounds(0, 0, 361, 127);
				{
					jLabel1 = new JLabel();
					jPanel1.add(jLabel1);
					jLabel1.setText("Select local bluetooth device for trasmission:");
					jLabel1.setBounds(7, 10, 306, 15);
					jLabel1.setFont(new java.awt.Font("AlArabiya",1,12));
				}
				{
					jLabel2 = new JLabel();
					jPanel1.add(jLabel2);
					jLabel2.setText("MAIN DEVICE");
					jLabel2.setBounds(100, 36, 94, 22);
					jLabel2.setFont(new java.awt.Font("AlArabiya",0,12));
				}
				
				{
					jLabel5 = new JLabel();
					jPanel1.add(jLabel5);
					jLabel5.setText("HELPER DEVICE");
					jLabel5.setBounds(100, 68, 101, 15);
					jLabel5.setFont(new java.awt.Font("AlArabiya",0,12));
				}
				{
					ComboBoxModel jComboBox1Model = 
						new DefaultComboBoxModel();
					mainDeviceCombo = new JComboBox();
					jPanel1.add(mainDeviceCombo);
					mainDeviceCombo.setModel(jComboBox1Model);
					mainDeviceCombo.setBounds(213, 36, 79, 21);
				}
			
				{
					jButton1 = new JButton();
					jPanel1.add(jButton1);
					jButton1.setText("Save");
					jButton1.setBounds(234, 91, 58, 25);
					
					ActionListener listener = new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {		
						BluetoothStack.getInstance().setMain((String) mainDeviceCombo.getSelectedItem());
						BluetoothStack.getInstance().setHelper((String) helperDeviceCombo.getSelectedItem());
					    setVisible(false);
					    dispose();
					    spm.setLocation((int)(toolkit.getScreenSize().getWidth()/2-spm.getWidth()/2),(int)(toolkit.getScreenSize().getHeight()/2-spm.getHeight()/2));	
					    spm.setVisible(true);
						}	
					};
					jButton1.addActionListener(listener );
					
				}
				{
					ComboBoxModel jComboBox2Model = 
						new DefaultComboBoxModel();
					helperDeviceCombo = new JComboBox();
					jPanel1.add(helperDeviceCombo);
					helperDeviceCombo.setModel(jComboBox2Model);
					helperDeviceCombo.setBounds(213, 64, 79, 21);
				}
				{
					jLabel3 = new JLabel(image);
					jPanel1.add(jLabel3);
					jLabel3.setBounds(16, 25, 65, 91);
				}
			}
			
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@SuppressWarnings("unchecked")
	public void getDevice(){
		try {
			Vector device = BlueCoveImpl.getLocalDevicesID();
			BlueCoveImpl.shutdown();
			device.size();
			for(int i=0;i<device.size();i++){
			mainDeviceCombo.addItem(device.elementAt(i));
			helperDeviceCombo.addItem(device.elementAt(i));
			}
			
		} catch (BluetoothStateException e) {
			e.printStackTrace();
		}	
			
		}		
	
	
	

}
